import WebSocket from 'ws'
import { Observable, merge, fromEvent } from 'rxjs'

export function createWebSocketClient(
  address: string,
  options?: WebSocket.ClientOptions
): Observable<any> {
  const ws = new WebSocket(address, options)
  const open = fromEvent(ws, 'open')
  const error = fromEvent(ws, 'error')
  const close = fromEvent(ws, 'close')
  const message = fromEvent(ws, 'message')
  const upgrade = fromEvent(ws, 'upgrade')
  const ping = fromEvent(ws, 'ping')
  const unexpectedResponse = fromEvent(ws, 'unexpected-response')
  return merge(open, error, close, message, upgrade, ping, unexpectedResponse)
}
